package com.memory.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.common.R;
import com.memory.daomain.Setmeal;
import com.memory.dto.SetmealDto;
import com.memory.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邓哈哈
 * 2023/1/27 11:18
 * Function:
 * Version 1.0
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    /**
     * 添加套餐
     * 添加对应菜品
     *
     * @param setmealDto
     * @return
     */
    @Autowired
    private SetMealService setMealService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        Boolean saveSetmealWithDish = setMealService.saveSetmealWithDish(setmealDto);
        return saveSetmealWithDish ? R.success("添加套餐完成!") : R.error("添加套餐失败!");
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        Boolean deleteSetmealWithDish = setMealService.deleteSetmealWithDish(ids);

        return deleteSetmealWithDish ? R.success("删除成功!") : R.error("删除失败!");
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        SetmealDto setmealDto = setMealService.getById(id);

        return setmealDto != null ? R.success(setmealDto) : R.error("回显失败!");
    }

    /**
     * 修改套餐信息
     * 修改对应菜品信息
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        Boolean updateSetmealWithDish = setMealService.updateSetmealWithDish(setmealDto);

        return updateSetmealWithDish ? R.success("修改成功!") : R.error("修改失败!");
    }

    /**
     * 修改套餐状态(停售?启售)
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long ids) {
        Setmeal setmeal = setMealService.getById(ids);

        if (status == 0)
            setmeal.setStatus(0);
        else
            setmeal.setStatus(1);

        boolean updateById = setMealService.updateById(setmeal);

        return updateById ? R.success("修改状态成功!") : R.error("修改状态失败");
    }


    /**
     * 分页查询
     * 条件查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> getPage(Integer page, Integer pageSize, Setmeal setmeal) {
        Page<SetmealDto> setmealDtoPage = setMealService.getPage(page, pageSize, setmeal);

        return setmealDtoPage != null ? R.success(setmealDtoPage) : R.error("查询失败");
    }

    /**
     * 根据套餐分类获取其下所有套餐
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        List<Setmeal> setmealList = setMealService.list(setmeal);

        return setmealList != null ? R.success(setmealList) : R.error("套餐显示失败");
    }
}
